package com.gitee.zzh.demo;

import com.gitee.zzh.common.MyIOUtils;
import com.gitee.zzh.common.QrCodeInfo;
import com.gitee.zzh.demo.service.QRCodeWithPictureService;
import com.gitee.zzh.demo.service.QrCodeService;
import com.gitee.zzh.demo.service.impl.QRCodeWithPictureServiceImpl;
import com.gitee.zzh.demo.service.impl.QrCodeServiceImpl;

import java.util.UUID;

public class QrCodeMainDemo {

    private static final String MAIN_DIR = "E:/java-spring/qrcode-demo/src/main";
    private static final String DIR = MAIN_DIR + "/resources/doc";

    public static void main(String[] args) {
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setQrCodeVersion(39);
        qrCodeInfo.setQrCodeMargin(1);
        qrCodeInfo.setMulti(5);
        QrCodeService qrCodeService = new QrCodeServiceImpl();
        //生成渐变色的二维码
        String content = MyIOUtils.getText(MAIN_DIR + "/java/com/example/exercise/demo/service/impl/QrCodeServiceImpl.java");
        qrCodeInfo.setQrCodePath(DIR + "/createQrCode.jpg");
        content = content.substring(0, 2200);
        qrCodeInfo.setQrCodeContent(content);
        qrCodeService.createQrCode(qrCodeInfo);
        //将二维码作为字符串保存到文件或者数据库
        qrCodeService.writeMatrixStrToFile(qrCodeInfo, DIR + "/qrCode.txt");
        //读取保存的字符串，用来还原生成二维码
        qrCodeService.createQrCodeFromMatrixStr(DIR + "/createQrCodeFromMatrixStr.jpg", DIR + "/qrCode.txt", 3);
        //识别本地二维码图片。这里只能识别生成的二维码，拍摄的图片一般会报错
        System.out.println(qrCodeService.getQrCodeContent(DIR + "/createQrCode.jpg"));
        /*=================================带图片的二维码==========================*/
        QRCodeWithPictureService qrCodeWithPictureService = new QRCodeWithPictureServiceImpl();
        //带图片的二维码，本案例使用了随机颜色
        //QRCodeWithPicture.encode("http://www.baidu.com", realPath + "/doc/inner.jpg", resp.getOutputStream(), true);
        qrCodeWithPictureService.encode("https://gitee.com/gava-coder/spring-boot-learn-v3.5.4?uuid=" + UUID.randomUUID(),
                DIR + "/inner.jpg", DIR + "/imgWithPic.jpg", true);
    }
}
